import Vue from "vue";
export default {
	namespaced: true, // 命名空间必须写
	state: {
		isLoading: true,
		bannerList: [],
		productCategoryList: [],
		medicalCosmetologyZoneList: [],
		recommendProductList: [],
		brandCategoryList: [],
		brandList: [],
		seckillList: [],
		crossBorderList: [],
		activityProductList: [],
		integralRecommendList: [],
		couponList: [],
		activityByScene: null
	},
	getters: {
		getState(state) {
			return state;
		}
	},
	// 唯一修改state值的方法 
	// commit
	mutations: {
		setData(state, val) {
			state.bannerList = val.bannerList
			// #ifdef MP-WEIXIN 
			state.productCategoryList = val.categoryList
			// #endif
			// #ifdef APP-PLUS || H5
			state.productCategoryList = val.categoryList.filter(item => item.name != '跨境专区');
			// #endif
			state.brandList = val.brandList
			state.medicalCosmetologyZoneList = val.medicalCosmetologyZoneList
			state.integralRecommendList = val.integralProduct;
			state.recommendProductList = val.recommendProduct;
			state.seckillList = val.seckillList
			state.crossBorderList = val.crossBorderList
			state.activityProductList = val.activityProductList
			state.brandCategoryList = val.brandCategoryList
			state.couponList = val.couponList
		},
		setIsLoading(state, val) {
			state.isLoading = val;
		},
		setActivityByScene(state, val) {
			state.activityByScene = val;
		},
		// setRecommendProduct(state, val) {
		// 	state.recommendProductList = val;
		// },
	},
	// 异步的操作
	actions: {
		// 获取主体信息
		getIndex(context) {
			Vue.prototype.$get("index/indexData").then(res => {
				context.commit('setData', res.data)
				context.dispatch('getActivityByScene')
				if (Vue.prototype.$isLogin() && context.rootState.user.user_type == 1) { // 用户登录
					// 获取用户信息
					context.dispatch('user/getUserInfo', 1, {
						root: true
					});
				}
				if (Vue.prototype.$isLogin() && context.state.couponList.length > 0) {
					setTimeout(() => {
						context.commit('messageBox/setVisible', true, {
							root: true
						});
					}, 2000);
				}
			}).catch(ex => {
				Vue.prototype.$errorToast(ex);
			})
		},
		// getRecommendProduct(context) {
		// 	Vue.prototype.$get("product/recommendList").then(res => {
		// 		console.log(res,'res')
		// 		context.commit('setRecommendProduct', res.data)
		// 		context.dispatch('getActivityByScene')
		// 	}).catch(ex => {
		// 		Vue.prototype.$errorToast(ex);
		// 	})
		// },
		getActivityByScene(context) {
			Vue.prototype.$get("redPacket/getActivityByScene", {
				receive_scene: 1
			}).then(res => {
				context.commit('setActivityByScene', res.data)
				context.commit('setIsLoading', false)
				// 获取个性化推荐商品
				context.dispatch('guessRecommend/getRecommendList', {}, {
					root: true
				});
			}).catch(ex => {
				Vue.prototype.$errorToast(ex);
			})
		},
	}
}